package com.yumei.trader.gym.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.yumei.trader.gym.R;
import com.yumei.trader.gym.util.DensityUtils;
import com.yumei.trader.gym.view.element.TicketElement;


public class KlineGameView extends KlineControlView {

    private float startY = 0.0f, startX = 0.0f;

    private String tag;

    private int mpad = 10;

    private int infoWidth = 100;
    private int infoHeight = 100;

    private int pointerMove = 40;

    //单指1，双指2
    private int modeNum = 0;
    private float preDistance;


    private Bitmap sellImgL, buyImgL;
    private Bitmap sellImgS, buyImgS;
    private float ms1P = 0;

    public KlineGameView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mpad = DensityUtils.dip2px(context, mpad);

        infoWidth = DensityUtils.dip2px(context, infoWidth);
        infoHeight = DensityUtils.dip2px(context, infoHeight);

        pointerMove = DensityUtils.dip2px(context, pointerMove);

        sellImgL = BitmapFactory.decodeResource(context.getResources(), R.mipmap.stock_sell_long);
        buyImgL = BitmapFactory.decodeResource(context.getResources(), R.mipmap.stock_buy_long);

        sellImgS = BitmapFactory.decodeResource(context.getResources(), R.mipmap.stock_sell_short);
        buyImgS = BitmapFactory.decodeResource(context.getResources(), R.mipmap.stock_buy_short);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub

        if (stock == null) return true;


        switch (event.getAction() & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_DOWN: { //单指触摸

                startX = event.getX();
                startY = event.getY();
                curIndex = (int) ((klineRect.right - rightPad - startX) / (cellWidth + dis));
                repaint();

                modeNum = 1;
            }
            break;

            case MotionEvent.ACTION_POINTER_DOWN: { //两指触摸

                preDistance = getDistance(event);
                //当两指间距大于10时，计算两指中心点
                if (preDistance > pointerMove / 2) {
                    modeNum = 2;
                }
                curIndex = -1;

            }
            break;

            case MotionEvent.ACTION_UP: //单手释放
                modeNum = 0;
                break;
            case MotionEvent.ACTION_POINTER_UP: //双手释放
                modeNum = 0;
                break;
            case MotionEvent.ACTION_MOVE: {

                //当两指缩放，计算缩放比例
                if (modeNum == 2) {
                    float distance = getDistance(event);
                    if (distance > preDistance + pointerMove) { //放大
                        upKline();
                    } else if (distance < preDistance - pointerMove) { //缩放
                        downKline();
                    }
                } else if (modeNum == 1) {
                    if (review) {
                        float dis = Math.abs(event.getX() - startX);
                        int cell = (int) (dis / cellWidth);

                        if (event.getX() - startX > cellWidth) { //左右移动
                            moveRight(cell);
                            startX = event.getX();

                        } else if (event.getX() - startX < -cellWidth) {
                            moveLeft(cell);
                            startX = event.getX();
                        }
                    }
                }
            }
            break;
        }
        return true;
    }

    /*获取两指之间的距离*/
    private float getDistance(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    }

    private void upKline() {

        int lenght = cells.length;
        index = (++index > lenght - 1) ? lenght - 1 : index;
        calucationScreenMaxTick(index);
    }

    private void downKline() {
        index = (--index < 0) ? 0 : index;
        calucationScreenMaxTick(index);
    }

    private void moveRight(int cell) {

        if (stock.tickets.size() > maxLen) {
            offset += cell;
            if (offset >= (stock.tickets.size() - maxLen) && offset > 0) {
                offset = stock.tickets.size() - maxLen;
            }
        } else {
            offset = 0;
        }

        calucationScreenMaxTick(index);
    }

    private void moveLeft(int cell) {

        if (stock.tickets.size() > maxLen || offset > 0) {
            offset = offset - cell;
            offset = (offset < 0) ? 0 : offset;
        }
        calucationScreenMaxTick(index);
    }

    protected void paint(Canvas canvas) {

        if (stock != null) {
            drawKlineBlock(canvas, klineRect.right - (cellWidth + dis) - rightPad);
            drawSelectTickDetail(canvas, klineRect.right - (cellWidth + dis) - rightPad);
        }

    }

    private void drawKlineBlock(Canvas canvas, float right) {

        int size = stock.tickets.size();

        canvas.save();
        canvas.clipRect(klineRect.left, klineRect.top, klineRect.right, klineRect.top + klineHeight);
        for (int i = 0; i < showLen; i++) {
            TicketElement tick = stock.tickets.get(size - 1 - offset - i);

            if (i == size - 1) {
                ms1P = 0;
            } else {
                if (size - offset - i - 2 >= 0) {
                    ms1P = stock.tickets.get((size - 1 - offset - i) - 1).ms1;
                }
            }

            if (neuron) { //神经元BS信号
                drawBSKlineCell(canvas, tick, i, right);
            } else {
                drawKlineCell(canvas, tick, i, right);
            }
        }
        canvas.restore();

        //画日期
        if (review) {
            paint.setColor(IColor.FONT);
            paint.setStyle(Paint.Style.FILL);
            TicketElement ticket = stock.tickets.get(size - 1 - offset);
            canvas.drawText(ticket.date, rect.right - rightPad - paint.measureText(ticket.date) - cellWidth / 2, rect.bottom - 5, paint);
            TicketElement ticket1 = stock.tickets.get(size - offset - showLen);
            canvas.drawText(ticket1.date, rect.right - rightPad - showLen * (cellWidth + dis) + cellWidth / 2, rect.bottom - 5, paint);
        }

        drawShare(canvas, right);

        paint.setStyle(Paint.Style.FILL);
        drawIndicationLine(canvas, right);
        paint.setTypeface(Typeface.DEFAULT);

        if (drawIndex) {
            paint.setColor(IColor.YELLOW);
            canvas.drawText(String.valueOf(offset), right - rect.right / 5, rect.top + paint.getTextSize(), paint);

            paint.setTextSize(FontSize14);
            paint.setTypeface(Typeface.DEFAULT_BOLD);
            TicketElement tick = stock.tickets.get(size - 1 - offset);
            if (tick.close >= tick.open) {
                paint.setColor(IColor.RED);
            } else {
                paint.setColor(IColor.GREEN2);
            }

            @SuppressLint("DefaultLocale")
            String price = String.format("%.2f", tick.close);
            canvas.drawText(price, right - paint.measureText(price), rect.top + paint.getTextSize(), paint);
            paint.setTextSize(FontSize12);
            paint.setTypeface(Typeface.DEFAULT);
        }
    }


    //画Ｋ线Ｃell
    private void drawKlineCell(Canvas canvas, TicketElement tick, int i, float right) {

        float bottomY = klineRect.top + klineHeight - 2;

        float maxY = bottomY - (tick.high - minPrice) * priceZoom;
        float minY = bottomY - (tick.low - minPrice) * priceZoom;
        float openY = bottomY - (tick.open - minPrice) * priceZoom;
        float closeY = bottomY - (tick.close - minPrice) * priceZoom;


        if (tick.close < tick.open) { //下跌填充矩形

            paint.setColor(IColor.GREEN2);
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2, maxY, right - i * (cellWidth + dis) - cellWidth / 2, minY, paint);

            paint.setColor(IColor.GREEN2);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawRect(right - i * (cellWidth + dis) - cellWidth, openY, right - i * (cellWidth + dis), closeY, paint);

            paint.setColor(IColor.GREEN2);
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(right - i * (cellWidth + dis) - cellWidth, openY, right - i * (cellWidth + dis), closeY, paint);

        } else if (tick.close > tick.open) { //上涨空心矩形

            paint.setColor(IColor.RED);
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2, maxY, right - i * (cellWidth + dis) - cellWidth / 2, minY, paint);

            paint.setColor(IColor.BLACK);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawRect(right - i * (cellWidth + dis) - cellWidth, openY, right - i * (cellWidth + dis), closeY, paint);

            paint.setColor(IColor.RED);
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(right - i * (cellWidth + dis) - cellWidth, openY, right - i * (cellWidth + dis), closeY, paint);
        } else {
            paint.setColor(IColor.WHITE2);
            canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2 - 1, maxY, right - i * (cellWidth + dis) - cellWidth / 2 - 1, minY, paint);
            canvas.drawLine(right - i * (cellWidth + dis) - cellWidth, openY, right - i * (cellWidth + dis), openY, paint);
        }

        if (tick.flag == 1) {
            if (tick.type.equals("Long")) {
                canvas.drawBitmap(buyImgL, right - i * (cellWidth + dis) - (cellWidth + buyImgL.getWidth()) / 2, minY + mpad, paint);
            } else if (tick.type.equals("Short")) {
                canvas.drawBitmap(buyImgS, right - i * (cellWidth + dis) - (cellWidth + buyImgS.getWidth()) / 2, minY + mpad, paint);
            }
        } else if (tick.flag == -1) {
            if (tick.type.equals("Long")) {
                canvas.drawBitmap(sellImgL, right - i * (cellWidth + dis) - (cellWidth + sellImgL.getWidth()) / 2, maxY - mpad - sellImgL.getHeight(), paint);
            } else if (tick.type.equals("Short")) {
                canvas.drawBitmap(sellImgS, right - i * (cellWidth + dis) - (cellWidth + sellImgS.getWidth()) / 2, maxY - mpad - sellImgS.getHeight(), paint);
            }
        } else {
            if (tick.date.equalsIgnoreCase(startDate)) {
                if (tick.type.equals("Long")) {
                    canvas.drawBitmap(buyImgL, right - i * (cellWidth + dis) - (cellWidth + buyImgL.getWidth()) / 2, minY + mpad, paint);
                } else if (tick.type.equals("Short")) {
                    canvas.drawBitmap(buyImgS, right - i * (cellWidth + dis) - (cellWidth + buyImgS.getWidth()) / 2, minY + mpad, paint);
                }
            }

            if (tick.date.equalsIgnoreCase(endDate)) {
                if (tick.type.equals("Long")) {
                    canvas.drawBitmap(sellImgL, right - i * (cellWidth + dis) - (cellWidth + sellImgL.getWidth()) / 2, maxY - mpad - sellImgL.getHeight(), paint);
                } else if (tick.type.equals("Short")) {
                    canvas.drawBitmap(sellImgS, right - i * (cellWidth + dis) - (cellWidth + sellImgS.getWidth()) / 2, maxY - mpad - sellImgS.getHeight(), paint);
                }
            }
        }

        //画游戏分隔线
        if (StockConst.STARTPOS == offset + i) {
            paint.setColor(0x88EFEFEf);
            paint.setStyle(Paint.Style.STROKE);
            paint.setPathEffect(effects);
            canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2 - 1, rect.top + 4, right - i * (cellWidth + dis) - cellWidth / 2 - 1, rect.bottom - 2, paint);
            paint.setPathEffect(null);
        }
    }


    //画BSＫ线Ｃell
    private void drawBSKlineCell(Canvas canvas, TicketElement tick, int i, float right) {

        float bottomY = klineRect.top + klineHeight - 2;

        float maxY = bottomY - (tick.high - minPrice) * priceZoom;
        float minY = bottomY - (tick.low - minPrice) * priceZoom;
        float openY = bottomY - (tick.open - minPrice) * priceZoom;
        float closeY = bottomY - (tick.close - minPrice) * priceZoom;


        if (tick.close < tick.open) { //下跌填充矩形

            if (tick.ms1 < 0) {
                paint.setColor(IColor.RED);
            } else if (tick.ms1 > 0) {
                paint.setColor(IColor.GREEN2);
            } else {
                paint.setColor(IColor.YELLOW);
            }

            paint.setStyle(Paint.Style.STROKE);
            canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2, maxY, right - i * (cellWidth + dis) - cellWidth / 2, minY, paint);

            paint.setStyle(Paint.Style.FILL);
            canvas.drawRect(right - i * (cellWidth + dis) - cellWidth, openY, right - i * (cellWidth + dis), closeY, paint);

            paint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(right - i * (cellWidth + dis) - cellWidth, openY, right - i * (cellWidth + dis), closeY, paint);

        } else if (tick.close > tick.open) { //上涨空心矩形

            if (tick.ms1 < 0) {
                paint.setColor(IColor.RED);
            } else if (tick.ms1 > 0) {
                paint.setColor(IColor.GREEN2);
            } else {
                paint.setColor(IColor.YELLOW);
            }

            paint.setStyle(Paint.Style.STROKE);
            canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2, maxY, right - i * (cellWidth + dis) - cellWidth / 2, minY, paint);

            paint.setColor(IColor.BLACK);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawRect(right - i * (cellWidth + dis) - cellWidth, openY, right - i * (cellWidth + dis), closeY, paint);

            if (tick.ms1 < 0) {
                paint.setColor(IColor.RED);
            } else if (tick.ms1 > 0) {
                paint.setColor(IColor.GREEN2);
            } else {
                paint.setColor(IColor.YELLOW);
            }

            paint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(right - i * (cellWidth + dis) - cellWidth, openY, right - i * (cellWidth + dis), closeY, paint);
        } else {

            if (tick.ms1 < 0) {
                paint.setColor(IColor.RED);
            } else if (tick.ms1 > 0) {
                paint.setColor(IColor.GREEN2);
            } else {
                paint.setColor(IColor.YELLOW);
            }

            canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2 - 1, maxY, right - i * (cellWidth + dis) - cellWidth / 2 - 1, minY, paint);
            canvas.drawLine(right - i * (cellWidth + dis) - cellWidth, openY, right - i * (cellWidth + dis), openY, paint);
        }

        if (ms1P <= 0 && tick.ms1 > 0) {
            canvas.drawBitmap(sellImgL, right - i * (cellWidth + dis) - (cellWidth + sellImgL.getWidth()) / 2, maxY - mpad - sellImgL.getHeight(), paint);
        } else if (ms1P >= 0 && tick.ms1 < 0) {
            canvas.drawBitmap(buyImgL, right - i * (cellWidth + dis) - (cellWidth + buyImgL.getWidth()) / 2, minY + mpad, paint);
        }

    }


    /**
     * 成交量
     *
     * @param canvas
     * @param right
     */
    private void drawShare(Canvas canvas, float right) {

        canvas.save();
        canvas.clipRect(volRect.left, volRect.top, volRect.right, volRect.top + klineHeight);

        int index = stock.tickets.size() - 1 - offset;

        for (int i = 0; i < showLen; i++) {
            TicketElement tick = stock.tickets.get(index - i);
            drawShareCell(canvas, tick, i, right);
        }

        canvas.restore();
    }


    //画成交量Ｃell
    private void drawShareCell(Canvas canvas, TicketElement tick, int i, float right) {

        float shareY = volRect.bottom - (tick.volume * shareZoom);

        if (tick.open > tick.close) {

            paint.setColor(IColor.GREEN2);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawRect(right - i * (cellWidth + dis) - cellWidth, shareY, right - i * (cellWidth + dis), volRect.bottom - 1, paint);
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(right - i * (cellWidth + dis) - cellWidth, shareY, right - i * (cellWidth + dis), volRect.bottom - 1, paint);
        } else {
            paint.setColor(IColor.BLACK);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawRect(right - i * (cellWidth + dis) - cellWidth, shareY, right - i * (cellWidth + dis), volRect.bottom - 1, paint);

            paint.setColor(IColor.RED);
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(right - i * (cellWidth + dis) - cellWidth, shareY, right - i * (cellWidth + dis), volRect.bottom - 1, paint);
        }

    }


    /**
     * 指标选择
     *
     * @param canvas
     * @param right
     */
    private void drawIndicationLine(Canvas canvas, float right) {

        for (int i = 0; i < request.indexs.length; i++) {

            switch (request.indexs[i]) {

                case StockConst.Index_Up_MA:
                    draw_MA_Ind(canvas, right);
                    break;
                case StockConst.Index_Up_SAR:
                    draw_SAR_Ind(canvas, right);
                    break;
                case StockConst.Index_Up_BOLL:
                    draw_Boll_Ind(canvas, right);
                    break;
                case StockConst.Index_Down_MACD:
                    draw_MACD_Ind(canvas, right);
                    break;
                case StockConst.Index_Down_RSI:
                    draw_RSI_Ind(canvas, right);
                    break;
                case StockConst.Index_Down_DMI:
                    draw_DMI_Ind(canvas, right);
                    break;
                case StockConst.Index_Down_KDJ:
                    draw_KDJ_Ind(canvas, right);
                    break;
                default:
                    break;
            }
        }
    }


    /**
     * MA 指标
     *
     * @param canvas
     * @param right
     */
    private void draw_MA_Ind(Canvas canvas, float right) {

        float bottomY = klineRect.top + klineHeight - 2;
        float endY = 0.0f;
        float startY = 0.0f;
        canvas.save();
        canvas.clipRect(klineRect.left, klineRect.top, klineRect.right, klineRect.top + klineHeight);

        int index = stock.tickets.size() - 1 - offset;

        for (int i = 0; i < showLen; i++) {

            if (index - i - 1 < 0) break;

            TicketElement ticket = stock.tickets.get(index - i);
            TicketElement temp = stock.tickets.get(index - i - 1);

            if (request.ma[0] > 0 && temp.MA5 > 0) { //MA5日

                startY = bottomY - (ticket.MA5 - minPrice) * priceZoom;
                endY = bottomY - (temp.MA5 - minPrice) * priceZoom;

                paint.setColor(IColor.MA5);
                canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);
            }

            if (request.ma[1] > 0 && temp.MA10 > 0) { //MA10

                startY = bottomY - (ticket.MA10 - minPrice) * priceZoom;
                endY = bottomY - (temp.MA10 - minPrice) * priceZoom;

                paint.setColor(IColor.MA10);
                canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);
            }

            if (request.ma[2] > 0 && temp.MA20 > 0) { //MA20

                startY = bottomY - (ticket.MA20 - minPrice) * priceZoom;
                endY = bottomY - (temp.MA20 - minPrice) * priceZoom;

                paint.setColor(IColor.MA20);
                canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);
            }

            if (request.ma[3] > 0 && temp.MA30 > 0) { //MA30

                startY = bottomY - (ticket.MA30 - minPrice) * priceZoom;
                endY = bottomY - (temp.MA30 - minPrice) * priceZoom;

                paint.setColor(IColor.MA30);
                canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);
            }

        }

        canvas.restore();
    }


    /**
     * MACD
     *
     * @param canvas
     */
    private void draw_MACD_Ind(Canvas canvas, float right) {

        float startY;
        float endY;
        float indZoom = indZooms[StockConst.Index_Down_MACD];
        int origY = indRect.bottom - (indHeight) / 2;


        canvas.save();
        canvas.clipRect(indRect.left + 1, indRect.top, indRect.right, indRect.bottom);


        paint.setColor(IColor.TABLE_DASHED);
        paint.setPathEffect(effects);
        canvas.drawLine(indRect.left + 1, origY, indRect.right, origY, paint);
        paint.setPathEffect(null);
        paint.setColor(IColor.FONT);
        canvas.drawText("0", indRect.right - paint.measureText("0") - 5, origY + 6, paint);


        int index = stock.tickets.size() - 1 - offset;

        for (int i = 0; i < showLen; i++) {

            if (index - i - 1 < 0) break;

            TicketElement ticket = stock.tickets.get(index - i);
            TicketElement temp = stock.tickets.get(index - i - 1);

            startY = origY - ticket.DIF * indZoom / 2;
            endY = origY - temp.DIF * indZoom / 2;

            paint.setColor(IColor.DIF);
            canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);

            startY = origY - ticket.DEA * indZoom / 2;
            endY = origY - temp.DEA * indZoom / 2;
            paint.setColor(IColor.DEA);
            canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);

            endY = origY - (ticket.MACD) * indZoom;
            if (ticket.MACD > 0) {
                paint.setColor(IColor.RED);
            } else {
                paint.setColor(IColor.GREEN2);
            }
            canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, origY, right - i * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);
        }


        canvas.restore();
    }


    /**
     * RSI
     *
     * @param canvas
     */
    private void draw_RSI_Ind(Canvas canvas, float right) {

        float startY;
        float endY;
        float indZoom = indZooms[StockConst.Index_Down_RSI];
        int origY = indRect.bottom;

        //画刻度线
        paint.setColor(IColor.TABLE);
        paint.setPathEffect(effects);

        canvas.drawLine(indRect.left + 1, origY - 50 * indZoom, indRect.right, origY - 50 * indZoom, paint); //50线
        canvas.drawLine(indRect.left + 1, origY - 70 * indZoom, indRect.right, origY - 70 * indZoom, paint); //70线
        canvas.drawLine(indRect.left + 1, origY - 30 * indZoom, indRect.right, origY - 30 * indZoom, paint); //30线
        paint.setPathEffect(null);


        paint.setColor(IColor.FONT);
        canvas.drawText("50", indRect.right - paint.measureText("50") - 2, origY - 50 * indZoom, paint);
        canvas.drawText("70", indRect.right - paint.measureText("70") - 2, origY - 70 * indZoom, paint);
        canvas.drawText("30", indRect.right - paint.measureText("30") - 2, origY - 30 * indZoom, paint);


        int index = stock.tickets.size() - 1 - offset;
        for (int i = 0; i < showLen; i++) {

            if (index - i - 1 < 0) break;

            TicketElement ticket = stock.tickets.get(index - i);
            TicketElement temp = stock.tickets.get(index - i - 1);

            if (request.rsi[0] > 0) {

                startY = origY - (ticket.RSI1) * indZoom;
                endY = origY - (temp.RSI1) * indZoom;
                paint.setColor(IColor.RSI1);
                canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);

            }

            if (request.rsi[1] > 0) {

                startY = origY - (ticket.RSI2) * indZoom;
                endY = origY - (temp.RSI2) * indZoom;
                paint.setColor(IColor.RSI2);
                canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);

            }

            if (request.rsi[2] > 0) {

                startY = origY - (ticket.RSI3) * indZoom;
                endY = origY - (temp.RSI3) * indZoom;
                paint.setColor(IColor.RSI3);
                canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);
            }
        }
    }


    /**
     * DMI
     *
     * @param canvas
     */
    private void draw_DMI_Ind(Canvas canvas, float right) {

        float startY = 0.0f;
        float endY = 0.0f;
        float pad = 0;
        float indZoom = indZooms[StockConst.Index_Down_DMI];


        paint.setColor(IColor.TABLE);
        paint.setPathEffect(effects);

        float y7 = indRect.bottom - 75 * indZoom;
        float y5 = indRect.bottom - 50 * indZoom;
        float y2 = indRect.bottom - 25 * indZoom;

        canvas.drawLine(indRect.left + 1, y7, indRect.right, y7, paint);
        canvas.drawLine(indRect.left + 1, y5, indRect.right, y5, paint);
        canvas.drawLine(indRect.left + 1, y2, indRect.right, y2, paint);

        paint.setPathEffect(null);
        paint.setColor(IColor.FONT);

        canvas.drawText("75", indRect.right - paint.measureText("75") - 2, y7 + 6, paint);
        canvas.drawText("50", indRect.right - paint.measureText("50") - 2, y5 + 6, paint);
        canvas.drawText("25", indRect.right - paint.measureText("25") - 2, y2 + 6, paint);

        int index = stock.tickets.size() - 1 - offset;

        for (int i = 0; i < showLen; i++) {

            if (index - i - 1 < 0) break;

            TicketElement ticket = stock.tickets.get(index - i);
            TicketElement temp = stock.tickets.get(index - i - 1);


            if (temp.PDI > 0) {

                startY = indRect.bottom - ticket.PDI * indZoom;
                endY = indRect.bottom - temp.PDI * indZoom;
                paint.setColor(IColor.PDI);

                canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);
            }

            if (temp.MDI > 0) {
                startY = indRect.bottom - ticket.MDI * indZoom;
                endY = indRect.bottom - temp.MDI * indZoom;
                paint.setColor(IColor.MDI);

                canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);
            }

            if (temp.ADX > 0) {
                startY = indRect.bottom - ticket.ADX * indZoom;
                endY = indRect.bottom - temp.ADX * indZoom;
                paint.setColor(IColor.ADX);
                canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);

            }
        }

    }


    /**
     * SAR
     *
     * @param canvas
     */
    private void draw_SAR_Ind(Canvas canvas, float right) {

        int bottomY = klineRect.top + klineHeight - 2;
        int index = stock.tickets.size() - 1 - offset;

        canvas.save();
        canvas.clipRect(klineRect.left + 2, klineRect.top + 2, klineRect.right - 2, klineRect.top + klineHeight);

        for (int i = 0; i < showLen; i++) {
            if (index - i - 1 < 0) break;

            TicketElement ticket = stock.tickets.get(index - i);

            int posY = bottomY - (int) ((ticket.SAR - minPrice) * priceZoom);

            if (ticket.SAR > ticket.close) {
                paint.setColor(IColor.GREEN);
                canvas.drawText("*", right - (i + 1) * (cellWidth + dis) + (cellWidth - 4) / 2, posY + paint.getTextSize() / 2, paint);
            } else {
                paint.setColor(IColor.PURPLE);
                canvas.drawText("*", right - (i + 1) * (cellWidth + dis) + (cellWidth - 4) / 2, posY + paint.getTextSize() / 2, paint);
            }
        }

        canvas.restore();

    }


    /**
     * Boll
     *
     * @param canvas
     */
    private void draw_Boll_Ind(Canvas canvas, float right) {

        canvas.save();
        canvas.clipRect(klineRect.left + 2, klineRect.top + 2, klineRect.right - 2, klineRect.top + klineHeight - 2);

        int bottomY = klineRect.top + klineHeight - 2;
        float startY, endY;

        int index = stock.tickets.size() - 1 - offset;

        for (int i = 0; i < showLen; i++) {

            if (index - i - 1 < 0) break;

            TicketElement ticket = stock.tickets.get(index - i);
            TicketElement temp = stock.tickets.get(index - i - 1);


            if (temp.M_Boll > 0) {
                startY = bottomY - (int) ((ticket.M_Boll - minPrice) * priceZoom);
                endY = bottomY - (int) ((temp.M_Boll - minPrice) * priceZoom);
                paint.setColor(IColor.WHITE);
                canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);
            }

            if (temp.U_Boll > 0) {
                startY = bottomY - (int) ((ticket.U_Boll - minPrice) * priceZoom);
                endY = bottomY - (int) ((temp.U_Boll - minPrice) * priceZoom);
                paint.setColor(IColor.YELLOW);
                canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);
            }

            if (temp.L_Boll > 0) {
                startY = bottomY - (int) ((ticket.L_Boll - minPrice) * priceZoom);
                endY = bottomY - (int) ((temp.L_Boll - minPrice) * priceZoom);
                paint.setColor(IColor.PURPLE);
                canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);
            }
        }

        canvas.restore();
    }


    /**
     * KDJ
     *
     * @param canvas
     */
    private void draw_KDJ_Ind(Canvas canvas, float right) {

        float startY, endY;
        float indZoom = indZooms[StockConst.Index_Down_KDJ];


        canvas.save();
        canvas.clipRect(indRect.left + 1, indRect.bottom - indHeight + 2, indRect.right, indRect.bottom);


        paint.setColor(IColor.TABLE);
        paint.setPathEffect(effects);

        float y7 = indRect.bottom - 80 * indZoom;
        float y5 = indRect.bottom - 50 * indZoom;
        float y2 = indRect.bottom - 20 * indZoom;

        canvas.drawLine(indRect.left + 1, y7, indRect.right, y7, paint);
        canvas.drawLine(indRect.left + 1, y5, indRect.right, y5, paint);
        canvas.drawLine(indRect.left + 1, y2, indRect.right, y2, paint);

        paint.setPathEffect(null);
        paint.setColor(IColor.FONT);

        canvas.drawText("80", indRect.right - paint.measureText("80") - 2, y7 + 6, paint);
        canvas.drawText("50", indRect.right - paint.measureText("50") - 2, y5 + 6, paint);
        canvas.drawText("20", indRect.right - paint.measureText("20") - 2, y2 + 6, paint);

        int index = stock.tickets.size() - 1 - offset;

        for (int i = 0; i < showLen; i++) {

            if (index - i - 1 < 0) break;

            TicketElement ticket = stock.tickets.get(index - i);
            TicketElement temp = stock.tickets.get(index - i - 1);

            if (temp.Kn > 0) {
                paint.setColor(IColor.WHITE);
                startY = indRect.bottom - ticket.Kn * indZoom;
                endY = indRect.bottom - temp.Kn * indZoom;
                canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);
            }

            if (temp.Dn > 0) {
                paint.setColor(IColor.YELLOW);
                startY = indRect.bottom - ticket.Dn * indZoom;
                endY = indRect.bottom - temp.Dn * indZoom;
                canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);
            }

            if (temp.Jn > -100) {
                paint.setColor(IColor.PURPLE);
                startY = indRect.bottom - ticket.Jn * indZoom;
                endY = indRect.bottom - temp.Jn * indZoom;
                canvas.drawLine(right - i * (cellWidth + dis) - cellWidth / 2.0f, startY, right - (i + 1) * (cellWidth + dis) - cellWidth / 2.0f, endY, paint);
            }
        }

        canvas.restore();
    }


    /**********************************************************************************************************
     * 画日K线选择详情
     * @param canvas
     * @param right
     */
    private void drawSelectTickDetail(Canvas canvas, float right) {

        if (!canClick) {
            return;
        }

        int size = stock.tickets.size();

        //无效选择
        if (curIndex < 0 || curIndex > showLen) {
            TicketElement ticket = stock.tickets.get(size - 1 - offset);
            showSelectedIndication(canvas, ticket);
            return;
        }

        //有效选择
        int index = size - 1 - offset - curIndex;
        if (index >= size || index < 0) {
            return;
        }

        TicketElement ticket = stock.tickets.get(index);

        float bottomY = klineRect.bottom;
        float X = right - curIndex * (cellWidth + dis) - cellWidth / 2.0f;
        float Y = bottomY - (ticket.open - minPrice) * priceZoom;

        float topX = X;
        float topY = Y;

        paint.setColor(IColor.WHITE2);
        canvas.drawLine(topX, rect.top, topX, rect.bottom, paint);

        if (topX + infoWidth > right) {
            topX = X - infoWidth - cellWidth;
        }
        if (topY + infoHeight > volRect.bottom) {
            topY = Y - infoHeight;
        }

        paint.setColor(IColor.BG);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRect(topX + cellWidth / 2, topY, topX + cellWidth / 2 + infoWidth, topY + infoHeight, paint);
        paint.setColor(IColor.WHITE2);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(topX + cellWidth / 2, topY, topX + cellWidth / 2 + infoWidth, topY + infoHeight, paint);

        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(FontSize12);

        //String time = date.substring(0,4)+"/"+date.substring(4,6)+"/"+date.substring(6);
        String time = String.valueOf(ticket.date);

        if (review) {
            paint.setColor(IColor.FONT);
            canvas.drawText(time, topX + 3 + cellWidth / 2, topY + 2 + FontSize12, paint);
        }

        float share = (ticket.volume / 10000);
        String shareStr;
        if (share / 10000 > 1) {
            shareStr = String.format("%.2f", share / 10000) + "亿";
        } else {
            shareStr = String.format("%.2f", share) + "万";
        }

        if (ticket.open < ticket.close) {

            paint.setColor(IColor.RED);

            canvas.drawText("open: " + String.format("%.2f", ticket.open), topX + 3 + cellWidth / 2, topY + 2 + FontSize12 + infoHeight / 6, paint);
            canvas.drawText("high: " + String.format("%.2f", ticket.high), topX + 3 + cellWidth / 2, topY + 2 + FontSize12 + 2 * infoHeight / 6, paint);
            canvas.drawText("low: " + String.format("%.2f", ticket.low), topX + 4 + cellWidth / 2, topY + 2 + FontSize12 + 3 * infoHeight / 6, paint);
            canvas.drawText("close: " + String.format("%.2f", ticket.close), topX + 3 + cellWidth / 2, topY + 2 + FontSize12 + 4 * infoHeight / 6, paint);
            //canvas.drawText("量: "+shareStr, topX+3+cellWidth/2, topY+2+FontSize12+5*infoHeight/6, paint);

        } else if (ticket.open > ticket.close) {
            paint.setColor(IColor.GREEN2);
            canvas.drawText("open: " + String.format("%.2f", ticket.open), topX + 3 + cellWidth / 2, topY + 2 + FontSize12 + infoHeight / 6, paint);
            canvas.drawText("high: " + String.format("%.2f", ticket.high), topX + 3 + cellWidth / 2, topY + 2 + FontSize12 + 2 * infoHeight / 6, paint);
            canvas.drawText("low: " + String.format("%.2f", ticket.low), topX + 4 + cellWidth / 2, topY + 2 + FontSize12 + 3 * infoHeight / 6, paint);
            canvas.drawText("close: " + String.format("%.2f", ticket.close), topX + 3 + cellWidth / 2, topY + 2 + FontSize12 + 4 * infoHeight / 6, paint);
            //canvas.drawText("量: "+shareStr, topX+3+cellWidth/2, topY+2+FontSize12+5*infoHeight/6, paint);

        } else {
            paint.setColor(IColor.WHITE2);
            canvas.drawText("open: " + String.format("%.2f", ticket.open), topX + 3 + cellWidth / 2, topY + 2 + FontSize12 + infoHeight / 6, paint);
            canvas.drawText("high: " + String.format("%.2f", ticket.high), topX + 3 + cellWidth / 2, topY + 2 + FontSize12 + 2 * infoHeight / 6, paint);
            canvas.drawText("low: " + String.format("%.2f", ticket.low), topX + 4 + cellWidth / 2, topY + 2 + FontSize12 + 3 * infoHeight / 6, paint);
            canvas.drawText("close: " + String.format("%.2f", ticket.close), topX + 3 + cellWidth / 2, topY + 2 + FontSize12 + 4 * infoHeight / 6, paint);
            //canvas.drawText("量: "+shareStr, topX+3+cellWidth/2, topY+2+FontSize12+5*infoHeight/6, paint);
        }

        showSelectedIndication(canvas, ticket);
    }


    /**
     * 显示选择K线指标数据
     *
     * @param canvas
     * @param ticket
     */
    private void showSelectedIndication(Canvas canvas, TicketElement ticket) {

        show_select_share(canvas, ticket);

        for (int i = 0; i < request.indexs.length; i++) {

            switch (request.indexs[i]) {

                case StockConst.Index_Up_MA:
                    show_select_ma(canvas, ticket);
                    break;
                case StockConst.Index_Up_SAR:
                    show_select_sar(canvas, ticket);
                    break;
                case StockConst.Index_Up_BOLL:
                    show_select_boll(canvas, ticket);
                    break;
                case StockConst.Index_Down_MACD:
                    show_select_macd(canvas, ticket);
                    break;
                case StockConst.Index_Down_RSI:
                    show_select_rsi(canvas, ticket);
                    break;
                case StockConst.Index_Down_DMI:
                    show_select_dmi(canvas, ticket);
                    break;
                case StockConst.Index_Down_KDJ:
                    show_select_kdj(canvas, ticket);
                    break;
                default:
                    break;
            }
        }
    }


    private void show_select_share(Canvas canvas, TicketElement ticket) {

        paint.setColor(IColor.YELLOW);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawText(" VOL:" + String.format("%.0f", ticket.volume), volRect.left, volRect.top + paint.getTextSize(), paint);

    }


    private void show_select_ma(Canvas canvas, TicketElement ticket) {

        float padding = 0;

        if (request.ma[0] > 0) {
            paint.setColor(IColor.MA5);
            tag = "MA" + request.ma[0] + ":" + String.format("%.2f", ticket.MA5);
            canvas.drawText(tag, klineRect.left + 10, klineRect.top + paint.getTextSize(), paint);
        }

        if (request.ma[1] > 0) {
            paint.setColor(IColor.MA10);
            tag = "MA" + request.ma[1] + ":" + String.format("%.2f", ticket.MA10);
            padding += paint.measureText(tag);
            canvas.drawText(tag, klineRect.left + padding, klineRect.top + paint.getTextSize(), paint);
        }

        if (request.ma[2] > 0) {
            paint.setColor(IColor.MA20);
            tag = "MA" + request.ma[2] + ":" + String.format("%.2f", ticket.MA20);
            padding += paint.measureText(tag) + 10;
            canvas.drawText(tag, klineRect.left + padding, klineRect.top + paint.getTextSize(), paint);
        }

        if (request.ma[3] > 0) {
            paint.setColor(IColor.MA30);
            tag = "MA" + request.ma[3] + ":" + String.format("%.2f", ticket.MA30);
            padding += paint.measureText(tag) + 10;
            canvas.drawText(tag, klineRect.left + padding, klineRect.top + paint.getTextSize(), paint);
        }

    }


    private void show_select_macd(Canvas canvas, TicketElement ticket) {

        int pad = 5;

        tag = "MACD(12,26,9)";
        paint.setColor(IColor.WHITE2);
        canvas.drawText(tag, indRect.left + pad, indRect.bottom - indHeight + paint.getTextSize(), paint);

        pad += paint.measureText(tag) + 5;
        tag = "DIF:" + String.format("%.2f", ticket.DIF);
        paint.setColor(IColor.DIF);
        canvas.drawText(tag, indRect.left + pad, indRect.bottom - indHeight + paint.getTextSize(), paint);

        pad += paint.measureText(tag) + 5;
        tag = "DEA:" + String.format("%.2f", ticket.DEA);
        paint.setColor(IColor.DEA);
        canvas.drawText(tag, indRect.left + pad, indRect.bottom - indHeight + paint.getTextSize(), paint);

        pad += paint.measureText(tag) + 5;
        tag = "MACD:" + String.format("%.2f", (ticket.MACD) * 2);
        paint.setColor(IColor.MACD);
        canvas.drawText(tag, indRect.left + pad, indRect.bottom - indHeight + paint.getTextSize(), paint);
    }


    private void show_select_rsi(Canvas canvas, TicketElement ticket) {

        int pad = 5;

        tag = "RSI(6,12,24)";
        paint.setColor(IColor.WHITE2);
        canvas.drawText(tag, indRect.left + pad, indRect.bottom - indHeight + paint.getTextSize(), paint);

        pad += paint.measureText(tag) + 5;
        tag = "RSI1:" + String.format("%.2f", ticket.RSI1);
        paint.setColor(IColor.RSI1);
        canvas.drawText(tag, indRect.left + pad, indRect.bottom - indHeight + paint.getTextSize(), paint);

        pad += paint.measureText(tag) + 5;
        tag = "RSI2:" + String.format("%.2f", ticket.RSI2);
        paint.setColor(IColor.RSI2);
        canvas.drawText(tag, indRect.left + pad, indRect.bottom - indHeight + paint.getTextSize(), paint);

        pad += paint.measureText(tag) + 5;
        tag = "RSI3:" + String.format("%.2f", ticket.RSI3);
        paint.setColor(IColor.RSI3);
        canvas.drawText(tag, indRect.left + pad, indRect.bottom - indHeight + paint.getTextSize(), paint);
    }


    private void show_select_dmi(Canvas canvas, TicketElement ticket) {

        int pad = 5;

        tag = "DMI(6,14)";
        paint.setColor(IColor.WHITE2);
        canvas.drawText(tag, indRect.left + pad, indRect.bottom - indHeight + paint.getTextSize(), paint);

        pad += paint.measureText(tag) + 5;
        tag = "PDI:" + String.format("%.2f", ticket.PDI);
        paint.setColor(IColor.PDI);
        canvas.drawText(tag, indRect.left + pad, indRect.bottom - indHeight + paint.getTextSize(), paint);

        pad += paint.measureText(tag) + 5;
        tag = "MDI:" + String.format("%.2f", ticket.MDI);
        paint.setColor(IColor.MDI);
        canvas.drawText(tag, indRect.left + pad, indRect.bottom - indHeight + paint.getTextSize(), paint);

        pad += paint.measureText(tag) + 5;
        tag = "ADX:" + String.format("%.2f", ticket.ADX);
        paint.setColor(IColor.ADX);
        canvas.drawText(tag, indRect.left + pad, indRect.bottom - indHeight + paint.getTextSize(), paint);
    }


    private void show_select_sar(Canvas canvas, TicketElement ticket) {

        int pad = 5;

        tag = "SAR(0.02, 0.2):";
        paint.setColor(IColor.WHITE2);
        canvas.drawText(tag, klineRect.left + pad, klineRect.top + paint.getTextSize(), paint);

        pad += paint.measureText(tag) + 5;
        tag = String.format("%.2f", ticket.SAR);

        if (ticket.SAR > ticket.close) {
            paint.setColor(IColor.GREEN);
        } else {
            paint.setColor(IColor.PURPLE);
        }
        canvas.drawText(tag, klineRect.left + pad, klineRect.top + paint.getTextSize(), paint);
    }


    private void show_select_boll(Canvas canvas, TicketElement ticket) {

        float pad = 5;
        tag = "BOLL:" + String.format("%.2f", ticket.M_Boll);
        paint.setColor(IColor.MBOLL);
        canvas.drawText(tag, klineRect.left + pad, klineRect.top + paint.getTextSize(), paint);

        pad += paint.measureText(tag) + 5;
        tag = "UPPER:" + String.format("%.2f", ticket.U_Boll);
        paint.setColor(IColor.UBOLL);
        canvas.drawText(tag, klineRect.left + pad, klineRect.top + paint.getTextSize(), paint);


        pad += paint.measureText(tag) + 5;
        tag = "LOWER:" + String.format("%.2f", ticket.L_Boll);
        paint.setColor(IColor.LBOLL);
        canvas.drawText(tag, klineRect.left + pad, klineRect.top + paint.getTextSize(), paint);

    }


    private void show_select_kdj(Canvas canvas, TicketElement ticket) {

        int pad = 5;

        tag = "KDL(9,3,3)";
        paint.setColor(IColor.WHITE2);
        canvas.drawText(tag, indRect.left + pad, indRect.bottom - indHeight + paint.getTextSize(), paint);

        pad += paint.measureText(tag) + 5;
        tag = "K:" + String.format("%.2f", ticket.Kn);
        paint.setColor(IColor.Kn);
        canvas.drawText(tag, indRect.left + pad, indRect.bottom - indHeight + paint.getTextSize(), paint);

        pad += paint.measureText(tag) + 5;
        tag = "D:" + String.format("%.2f", ticket.Dn);
        paint.setColor(IColor.Dn);
        canvas.drawText(tag, indRect.left + pad, indRect.bottom - indHeight + paint.getTextSize(), paint);

        pad += paint.measureText(tag) + 5;
        tag = "J:" + String.format("%.2f", ticket.Jn);
        paint.setColor(IColor.Jn);
        canvas.drawText(tag, indRect.left + pad, indRect.bottom - indHeight + paint.getTextSize(), paint);
    }


}
